package com.hualan.web.wsj;

import com.hualan.pojo.CategoryCount;
import com.hualan.pojo.wsj.Cpfl;
import com.hualan.service.service.service.wsj.CpflService;
import com.hualan.service.service.service.wsj.CptmService;
import com.hualan.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Cpfl")
public class CpflController {
    @Autowired(required=false)
    private CpflService cpflService;
    @Autowired(required=false)
    private CptmService cptmService;

    @RequestMapping("/listAll")
    public ResultEntity listAll() {
//        获取所有心理分类数据
        List<Cpfl> cpflList = cpflService.listAll();
//        获取分类下的题目数量
        List<CategoryCount> categoryCounts = cptmService.countByCategory();

        for(Cpfl cpfl:cpflList){
            for(CategoryCount categoryCount:categoryCounts){
                if(cpfl.getId()==categoryCount.getCategory()){
                    cpfl.setCount(categoryCount.getCount());
                    break;
                }
            }
        }
        return ResultEntity.ok(cpflList);
    }

    // 返回每个分类的题目数量
    @RequestMapping("/categoryCount")
    public ResultEntity categoryCount() {
        List<CategoryCount> categoryCounts = cptmService.countByCategory();
        return ResultEntity.ok(categoryCounts);
    }

}
